package com.bocai.freeman.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bocai.freeman.R;
import com.bocai.freeman.model.bean.EventMessage;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by win7 on 2016/1/26.
 * 获取图片
 */
public class PictureGridAdapter extends BaseAdapter {
    private Context context;
    private List<String> pictureList;
    private int mNum;

    public PictureGridAdapter(Context context, List<String> pictureList, int num) {
        this.context = context;
        this.pictureList = pictureList;
        this.mNum = num;
    }

    public void upDateList(List<String> pictureList) {
        this.pictureList = pictureList;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (pictureList.size() == mNum) {
            return mNum;
        }
        return (pictureList.size() + 1);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_picture_gridview, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == pictureList.size()) {
            holder.imgDelete.setVisibility(View.INVISIBLE);
            Glide.with(context)
                    .load(R.mipmap.ic_add_img)
                    .bitmapTransform(new RoundedCornersTransformation(context, 15, 0, RoundedCornersTransformation.CornerType.ALL))
                    .into(holder.itemGridaImage);
            if (position == (mNum + 1)) {
                holder.itemGridaImage.setVisibility(View.GONE);
            }
        } else {
            holder.imgDelete.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(pictureList.get(position))
                    .bitmapTransform(new RoundedCornersTransformation(context, 15, 0, RoundedCornersTransformation.CornerType.ALL))
                    .into(holder.itemGridaImage);
        }

        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventMessage.sendMessage("deleteImg", position);
            }
        });

        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.item_grida_image)
        ImageView itemGridaImage;
        @Bind(R.id.img_delete)
        ImageView imgDelete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
